package com.wang.springbootinit.controller.teacher;

import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.model.entity.ResearchChapter;
import com.wang.springbootinit.model.entity.ResearchSubject;
import com.wang.springbootinit.model.entity.ResearchUniversity;
import com.wang.springbootinit.service.ResearchChapterService;
import com.wang.springbootinit.service.ResearchSubjectService;
import com.wang.springbootinit.service.ResearchUniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.List;

/**
 * 考研信息管理控制器
 * 负责处理院校信息、统考科目、章节等相关的CRUD操作
 *
 * @author [你的名字]
 */
@RestController
@RequestMapping("/teacher/researchInfo")
@Validated
public class ResearchInfoController {

    @Autowired
    private ResearchUniversityService universityService;
    
    @Autowired
    private ResearchSubjectService subjectService;
    
    @Autowired
    private ResearchChapterService chapterService;



    /**
     * 添加院校信息
     * @param university 院校信息对象
     * @return 新增院校的ID
     */
    @PostMapping("/university/add")
    public BaseResponse<Boolean> addUniversity(@RequestBody ResearchUniversity university) {
        return ResultUtils.success(universityService.save(university));
    }

    /**
     * 更新院校信息
     * @param university 需要更新的院校信息
     * @return 更新是否成功
     */
    @PostMapping("/university/update")
    public BaseResponse<Boolean> updateUniversity(@RequestBody ResearchUniversity university) {
        return ResultUtils.success(universityService.updateById(university));
    }

    /**
     * 删除院校信息
     * @param id 院校ID
     * @return 删除是否成功
     */
    @PostMapping("/university/delete/{id}")
    public BaseResponse<Boolean> deleteUniversity(@PathVariable("id") long id) {
        return ResultUtils.success(universityService.removeById(id));
    }

    /**
     * 获取所有院校信息列表
     * @return 院校信息列表
     */
    @GetMapping("/university/list")
    public BaseResponse<List<ResearchUniversity>> listUniversities() {
        return ResultUtils.success(universityService.list());
    }

    /**
     * 添加统考科目
     * @param subject 统考科目信息
     * @return 新增科目的ID
     */
    @PostMapping("/subject/add")
    public BaseResponse<Boolean> addSubject(@RequestBody ResearchSubject subject) {
        return ResultUtils.success(subjectService.save(subject));
    }

    /**
     * 更新统考科目信息
     * @param subject 需要更新的科目信息
     * @return 更新是否成功
     */
    @PostMapping("/subject/update")
    public BaseResponse<Boolean> updateSubject(@RequestBody ResearchSubject subject) {
        return ResultUtils.success(subjectService.updateById(subject));
    }

    /**
     * 删除统考科目
     * @param id 科目ID
     * @return 删除是否成功
     */
    @PostMapping("/subject/delete/{id}")
    public BaseResponse<Boolean> deleteSubject(@PathVariable("id") long id) {
        return ResultUtils.success(subjectService.removeById(id));
    }

    /**
     * 获取所有统考科目列表
     * @return 统考科目列表
     */
    @GetMapping("/subject/list")
    public BaseResponse<List<ResearchSubject>> listSubjects() {
        return ResultUtils.success(subjectService.list());
    }

    @GetMapping("/subject/get/{id}")
    public BaseResponse<ResearchSubject> getSubjectById(@PathVariable("id") long id) {
        return ResultUtils.success(subjectService.getById(id));
    }

    /**
     * 根据学校id查询下面的所有科目
     * @return 统考科目列表
     */
    @GetMapping("/subject/getSchool/{schoolId}")
    public BaseResponse<List<ResearchSubject>> listSubjectsBySchoolId(@PathVariable("schoolId") Long schoolId) {
        return ResultUtils.success(subjectService.listSubjectsBySchoolId(schoolId));
    }

    /**
     * 添加章节信息
     * @param chapter 章节信息
     * @return 新增章节的ID
     */
    @PostMapping("/chapter/add")
    public BaseResponse<Boolean> addChapter(@RequestBody ResearchChapter chapter) {
        return ResultUtils.success(chapterService.save(chapter));
    }

    /**
     * 更新章节信息
     * @param chapter 需要更新的章节信息
     * @return 更新是否成功
     */
    @PostMapping("/chapter/update")
    public BaseResponse<Boolean> updateChapter(@RequestBody ResearchChapter chapter) {
        return ResultUtils.success(chapterService.updateById(chapter));
    }

    /**
     * 删除章节
     * @param id 章节ID
     * @return 删除是否成功
     */
    @PostMapping("/chapter/delete/{id}")
    public BaseResponse<Boolean> deleteChapter(@PathVariable("id") long id) {
        return ResultUtils.success(chapterService.removeById(id));
    }

    /**
     * 根据科目ID获取相关章节列表
     * @param subjectId 科目ID
     * @return 指定科目的章节列表
     */
    @GetMapping("/chapter/list")
    public BaseResponse<List<ResearchChapter>> listChapters(@RequestParam long subjectId) {
        return ResultUtils.success(chapterService.listBySubjectId(subjectId));
    }


    /**
     * 根据科目id查询章节
     * @param id 科目id
     * @return
     */
    @GetMapping("/chapter/getBySubject/{id}")
    public BaseResponse<List<ResearchChapter>> getChapterBySubjectId(@PathVariable("id") long id) {
        return ResultUtils.success(chapterService.getChapterBySubjectId(id));
    }
}
